package com.gravata.enigma.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.gravata.enigma.R;
import com.gravata.enigma.ReloadDataListener;
import com.gravata.enigma.activities.TelaInicial;
import com.gravata.enigma.model.Profile;

public class ProfileAdapter extends ArrayAdapter<Profile>{
	private Context context;
	private List<Profile> profiles;
	private LayoutInflater mInflater;
	private int originalColor=-1;
	private Map<Integer, Boolean> selecionados=new HashMap<Integer, Boolean>();

	private ReloadDataListener dataListener;

	public ProfileAdapter(Context context, List<Profile> profiles, ReloadDataListener dataListener) {
		super(context, R.layout.profile_lista, profiles);
		this.context=context;
		this.profiles=profiles;
		this.dataListener=dataListener;
		init(context);
	}

	private void init(Context context) {
		this.mInflater = LayoutInflater.from(context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder vh;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.profile_item, parent, false);
			vh = new ViewHolder(convertView);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}

		convertView.setBackgroundColor(getOriginalColor(convertView)); //default color
        if (selecionados.get(position) != null) {
        	 convertView.setBackgroundColor(convertView.getResources().getColor(R.color.pressed_color));// this is a selected position so make it red
        }

        final Profile profile=getItem(position);

		vh.textNome.setText(profile.getNome());
		vh.textEndereco.setText(profile.getUrl());

		TextView selecionado=(TextView) convertView.findViewById(R.id.selecionado);

		if(profile.isSelecionado()){
			selecionado.setBackgroundDrawable(selecionado.getResources().getDrawable(android.R.drawable.checkbox_on_background));
		}else{
			selecionado.setBackgroundDrawable(selecionado.getResources().getDrawable(android.R.drawable.checkbox_off_background));
		}

		selecionado.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				TelaInicial.repositorioProfile.setSelecionado(profile);
				setData(TelaInicial.repositorioProfile.listar());
				TelaInicial.profile=profile;
				dataListener.onReloadData();
			}
		});

		return convertView;
	}


	public void setData(List<Profile> profiles){
		this.profiles.clear();

		for(Profile t:profiles){
			this.profiles.add(t);
		}

		notifyDataSetChanged();
	}

	static class ViewHolder {

		TextView textNome;
		TextView textEndereco;
		private ViewHolder(View rootView) {
			textNome = (TextView) rootView.findViewById(R.id.nome);
			textEndereco=(TextView) rootView.findViewById(R.id.endereco);

		}
	}

    public void setSelection(int position, boolean checked){
    	if(checked)
    		selecionados.put(position, checked);
    	else
        	selecionados.remove(position);

    	notifyDataSetChanged();
    }


    public void clearSelection(){
    	selecionados=new HashMap<Integer, Boolean>();
    	notifyDataSetChanged();
    }

    private int getOriginalColor(View view){
    	if(originalColor==-1){
	        originalColor = Color.TRANSPARENT;
	        Drawable background = view.getBackground();
	        if (background instanceof ColorDrawable)
	        	originalColor = ((ColorDrawable) background).getColor();
    	}
    	return originalColor;

    }

}
